# coding: utf-8

from marshmallow import fields

from ..extensions import ma
from ..schemas import BaseModelSchema
from app.enterprise.models import (
    Enterprise, EnterpriseHomepage, EnterpriseProject,
    EnterpriseSecurity, EnterpriseProduct, EnterpriseMediaReport,
    EnterprisePicture, EnterpriseHonour
)


class EnterpriseProjectSchema(BaseModelSchema):
    """
    add by zhouhl 2018-05-22
    """

    class Meta:
        model = EnterpriseProject
        exclude = ('id', 'enterprise_id', 'enterprise', 'created_at', 'updated_at', 'deleted_at', 'project_type')

    project_type_display = fields.Str(dump_only=True)
    project_img = fields.Str(attribute='project_img_url', dump_only=True)


class EnterpriseSecuritySchema(BaseModelSchema):
    """
    add by zhouhl 2018-05-22
    """

    class Meta:
        model = EnterpriseSecurity
        exclude = ('id', 'enterprise_id', 'enterprise', 'created_at', 'updated_at', 'deleted_at', 'security_system')

    security_display = fields.Str(attribute='security_display', dump_only=True)


class EnterpriseProductSchema(BaseModelSchema):
    """
    add by zhouhl 2018-05-22
    """

    class Meta:
        model = EnterpriseProduct
        exclude = ('id', 'enterprise_id', 'enterprise', 'created_at', 'updated_at', 'deleted_at', 'product_type')

    product_type_display = fields.Str(dump_only=True)
    product_img = fields.Str(attribute='product_img_url', dump_only=True)


class EnterpriseMediaReportSchema(BaseModelSchema):
    """
    add by zhouhl 2018-05-22
    """

    class Meta:
        model = EnterpriseMediaReport
        exclude = ('id', 'enterprise_id', 'enterprise', 'created_at', 'updated_at', 'deleted_at')

    report_img = fields.Str(attribute='report_img_url', dump_only=True)


class EnterprisePictureSchema(BaseModelSchema):
    """
    add by zhouhl 2018-05-22
    """

    class Meta:
        model = EnterprisePicture
        exclude = ('id', 'enterprise_id', 'enterprise', 'created_at', 'updated_at', 'deleted_at')

    picture_img = fields.Str(attribute='picture_img_url', dump_only=True)


class EnterpriseHonourSchema(BaseModelSchema):
    """
    add by zhouhl 2018-05-22
    """

    class Meta:
        model = EnterpriseHonour
        exclude = ('id', 'enterprise_id', 'enterprise', 'created_at', 'updated_at', 'deleted_at')

    honour_img = fields.Str(attribute='honour_img_url', dump_only=True)


class EnterpriseHomepageSchema(BaseModelSchema):
    """
    add by zhouhl 2018-05-22
    金银牌推广首页相关信息
    """

    class Meta:
        model = EnterpriseHomepage
        exclude = ('id', 'enterprise_id', 'enterprise', 'created_at', 'updated_at', 'deleted_at')

    header_logo = fields.Str(attribute='header_logo_url', dump_only=True)
    footer_logo = fields.Str(attribute='footer_logo_url', dump_only=True)
    first_screen_background = fields.Str(attribute='first_screen_background_url', dump_only=True)
    partner_img = fields.Str(attribute='partner_img_url', dump_only=True)
    legal_person_avatar = fields.Str(attribute='legal_person_avatar_url', dump_only=True)
    virtual_number_img = fields.Str(attribute='virtual_number_img_url', dump_only=True)


class EnterpriseSchema(BaseModelSchema):
    """
    """
    class Meta:
        model = Enterprise
        fields = (
            'id', 'enterprise_name', 'enterprise_address',
            'homepage', 'projects', 'securities', 'products',
            'media_reports', 'pictures', 'honours'
        )

    homepage = fields.Nested(EnterpriseHomepageSchema, many=False)
    projects = fields.Nested(EnterpriseProjectSchema, many=True)
    securities = fields.Nested(EnterpriseSecuritySchema, many=True)
    products = fields.Nested(EnterpriseProductSchema, many=True)
    media_reports = fields.Nested(EnterpriseMediaReportSchema, many=True)
    pictures = fields.Nested(EnterprisePictureSchema, many=True)
    honours = fields.Nested(EnterpriseHonourSchema, many=True)


enterprise_schema = EnterpriseSchema()


class EnterpriseImSchema(ma.Schema):
    """
    created by chenhj at 2018/5/29

    """
    from_id = fields.Str(required=True, error_messages={
        'required': '发送方ID不合法'
    })
    to_id = fields.Str(required=True, error_messages={
        'required': '接收方ID不合法'
    })
    content = fields.Str(required=True, error_messages={
        'required': '发送内容不合法'
    })

enterprise_im_schema = EnterpriseImSchema()
